Home:ALL Converter>Using Autocomplete to search Files

Using Autocomplete to search Files

Ask Time:2012-06-22T04:37:35         Author:Ricardo Binns

Json Formatter

i'm trying to use autocomplete to search some files, but its not working and i dont know why.

will make a example of a simple autocomplete working with a database result:

$items = array();
while(!$res->EOF){

    $descPi  = "PI: ".$res->fields["FIELD1"];
    $descEmb = " - EMB: ".$res->fields["FIELD2"]."/".$res->fields["FIELD3"]."/".$res->fields['FIELDS4'];
    $origem  = " - ORI: ".$res->fields['FIELD5'];
    $fornec  = " - FOR: ".$res->fields['FIELD6'];   

    $items[$descPi.$descEmb.$origem.$fornec]  = $res->fields["FIELD1"];
    $res->MoveNext();
}

this work perfect, outputting a array like this (by firebug):

Array
  (
    [PI: 3221554 - EMB: 30/6/2012 - ORI: TAIWAN - FOR: SIRUBA] => 3221554
   )
[ { "id": "3221554", "label": "PI: 3221554 - EMB: 30/6/2012 - ORI: TAIWAN - FOR: SIRUBA", "value": "PI: 3221554 - EMB: 30/6/2012 - ORI: TAIWAN - FOR: SIRUBA" } ]

perfect.


And now i have almost the same, but searching files, and its not working:

$items = array();
for($j=0;$j<count($files);$j++)
{
    if($files[$j] != "." and $files[$j] != ".."){
        $items[$files[$j]]  = $files[$j];
    }
}

and output this (by firebug):

Array
 (
   [Dock.jpg] => Dock.jpg
   [Forest Flowers.jpg] => Forest Flowers.jpg
   [Forest.jpg] => Forest.jpg
   [Tree.jpg] => Tree.jpg
 )
 [  ] // return's empty

debugging the code, i found this strpos validation and he say's that my array its returning false, not entering to make the array_push:

$result = array();
foreach ($items as $key=>$value) {
if (strpos(strtolower($key), $q) !== false) {
    array_push($result, array("id"=>$value, "label"=>$key, "value" => strip_tags($key)));
}
if (count($result) > 11)
    break;
}

so, comparing the two arrays , i did not found any diference, what i'm missing ?

that's it, any question, be my guest.

Author:Ricardo Binns,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/11146344/using-autocomplete-to-search-files
yy